package com.example.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.auth.model.MenuModel;
import com.example.auth.model.RoleModel;
import com.example.auth.service.MenuModelService;
import com.example.auth.service.RoleModelService;
import com.example.auth.service.UserModelService;
import com.example.db.model.IPage;
import com.example.utils.Results;

import java.util.Date;
import java.util.List;

/**
 * @Classname RoleController
 * @Description TODO
 * @Date 2022/7/29 17:08
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController(value = "/role",desc = "角色管理模块")
public class RoleController {

    @Autowired
    private UserModelService userModelService;

    @Autowired
    private RoleModelService roleModelService;

    @PostMapping("/save")
    public JSONObject saveMenuUserModelController(@RequestBody RoleModel roleModel){
        if(roleModel==null){
            return Results.error("新建的信息不得为空");
        }
        String roleno=roleModel.getRoleno();
        RoleModel role=roleModelService.getRoleModelByRoleNo(roleno);
        if(role!=null){
            return Results.error("角色已经存在");
        }
        roleModel.setCreatedate(new Date());
        roleModel.setIsdel(0);
        roleModelService.saveRoleModel(roleModel);
        return Results.success("新建成功");
    }

    @PostMapping(value = "/update",desc = "角色更新")
    public JSONObject updateRoleModelController(@RequestBody RoleModel roleModel){
        if(roleModel==null){
            return Results.error("重要信息不得为空");
        }
        RoleModel role=roleModelService.getRoleModelById(String.valueOf(roleModel.getId()));
        if(role==null){
            return Results.error("角色信息不存在");
        }
        roleModelService.updateRoleModel(roleModel);
        return Results.success("角色更新成功");
    }

    @GetMapping(value = "/getone",desc = "角色获取一个")
    public JSONObject getRoleModelController(@Param("id")String id){
        RoleModel roleModel=roleModelService.getRoleModelById(id);
        return Results.success(roleModel);
    }

    @PostMapping(value = "/page",desc = "角色分页")
    public JSONObject getRoleModelPageControoler(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                                 @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                                 @Param(value = "searchParams",required = false)JSONObject object){
        IPage<RoleModel> roleModelIPage=roleModelService.getRoleModelPage(pageNum,pageSize,object);
        return Results.successLayuiTable(roleModelIPage);
    }

    @PostMapping("/getall")
    public JSONObject getAllMenuListController(){
        List<RoleModel> roleModelList=roleModelService.getRoleModelList();
        return Results.success(roleModelList);
    }
}
